package com.cn.web.app.baidu;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.config.OrderConfig;
import com.cn.upload.base.FileItemEntity;
import com.cn.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * description: FastDfsFileUpload <br>
 * date: 24.7.14 15:54 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@Service
@Slf4j
public class FastDfsFileUpload {

    @Autowired
    private OrderConfig orderConfig;

    public FileItemEntity upload(Map<String, Object> paramMap) {
        paramMap.put("path", DateUtil.getTime("yyyy-MM-dd"));
        paramMap.put("output", "json");
        String url = orderConfig.getFileDomain();
        String result = HttpUtil.post(url + "/upload?auth_token=3", paramMap);
        log.error(result);
        JSONObject jsonObject = JSON.parseObject(result);
        String domain = jsonObject.getString("domain");
        String path = jsonObject.getString("path");
        long size = jsonObject.getLong("size");
        FileItemEntity fileItemEntity = new FileItemEntity();
        fileItemEntity.setFileDomain(domain).setUrl(path).setSize(size + "");
        return fileItemEntity;
    }

}
